import axios from 'axios'
import qs from 'qs'
import { Message } from 'element-ui'
axios.interceptors.request.use(
    config => {
        if (localStorage.getItem('fa_token')) {
            // 添加headers
            config.headers.Authorization = localStorage.getItem('fa_token')
            config.headers['content-type'] = 'application/json'
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
)
axios.defaults.withCredentials = true
axios.defaults.baseURL = process.env.VUE_APP_API_ROOT
console.log(process.env.VUE_APP_API_ROOT, 'dfnieodnfhieuo')
axios.interceptors.response.use(
    response => {
        return response
    },
    error => {
        return Promise.resolve(error.response)
    }
)

function checkStatus(response) {
    let res = response
    if (res && res.status && res.status == 401) {
        return Promise.reject('token验证错误！')
    }
    if (res && res.status && res.status == 400) {
        Message.error(res.data.msg)
        // 可能需要重新获取token
        return Promise.reject('token验证错误！')
    }
    if (
        response &&
    (response.status === 200 ||
      response.status === 304 ||
      response.status === 400)
    ) {
        return response
    }
    return {
        status: -404,
        msg: '网络异常'
    }
}
function checkCode(res) {
    if (res && res.data && res.data.code === 500) {
        Message.error(res.data.msg)

    }
    return res
}
export default {
    post(url, data) {
        return axios({
            method: 'post',
            url,
            data: qs.stringify(data),
            withCredentials: true,
            timeout: 300000,
            dataType: 'json',
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        }).then(response => {
            return checkStatus(response)
        })
    },
    get(url, params) {
        return axios({
            method: 'get',
            url,
            params,
            withCredentials: true,
            timeout: 300000,
            headers: {
                'X-Requested-With': 'XMLHttpRequest'
            }
        }).then(response => {
            return checkStatus(response)
        })
    },
    getImage(url, data) {
        return axios({
            method: 'post',
            url,
            data: data,
            withCredentials: true,
            timeout: 300000,
            dataType: 'json',
            headers: {},
            responseType: 'arraybuffer'
        }).then(response => {
            return checkStatus(response)
        })
    },
    postJson(url, data) {
        return axios({
            method: 'post',
            url,
            data: data,
            withCredentials: true,
            timeout: 300000,
            dataType: 'json',
            headers: {
                // tenantId:111
            }
        }).then(response => {
            return checkStatus(response)
        })
    },
    postFormData(url, data) {
        return axios({
            method: 'post',
            url,
            data: data,
            withCredentials: true,
            timeout: 300000,
            dataType: 'json',
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        }).then(response => {
            return checkStatus(response)
        })
    },
    postBody(url, data) {
        return axios({
            method: 'POST',
            url,
            data: data,
            timeout: 300000,
            dataType: 'json',
            headers: {
                'Content-Type': 'application/json'
            },
            withCredentials: true
        })
            .then(response => {
                return checkStatus(response)
            })
            .then(res => {
                return checkCode(res)
            })
    },
    put(url, data) {
        return axios({
            method: 'PUT',
            url,
            data: data,
            withCredentials: true,
            timeout: 300000,
            dataType: 'json',
            headers: {}
        }).then(response => {
            return checkStatus(response)
        })
    },
    deletefn(url, data) {
        return axios({
            method: 'delete',
            url,
            data: data,
            withCredentials: true,
            dataType: 'json',
            headers: {},
            timeout: 300000
        }).then(response => {
            return checkStatus(response)
        })
    }
}
